package com.p2p.service.operationsManagementService.impl;

import com.p2p.common.base.RespResult;
import com.p2p.entity.ExperienceConfiguration;
import com.p2p.mapper.ExperienceMapper;
import com.p2p.service.operationsManagementService.ExperienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class ExperienceServiceImpl implements ExperienceService {

    @Autowired
    private ExperienceMapper experienceMapper;

    @Override
    @Transactional
    public RespResult addExperience(ExperienceConfiguration experienceConfiguration) {
        int i = experienceMapper.addExperience(experienceConfiguration);
        if (i > 0){
            BigDecimal amount = experienceConfiguration.getAmount();
            BigDecimal rate = experienceConfiguration.getRate();
            Long id = experienceConfiguration.getId();
            if (amount != null && rate != null){
                BigDecimal income = amount.multiply(rate);
                experienceMapper.addIncome(id,income);
            }
            return RespResult.success();
        }
        return  RespResult.error("服务器开小差了");
    }
}
